package com.ja.openchat.ui.found;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.adapters.CircleHomeAdapter;
import com.ja.openchat.ui.base.BaseLazyLoadFragment;
import com.ja.openchat.views.ExLinearLayoutManager;
import com.zy.openchat.util.Common;

import java.util.List;

import butterknife.BindView;

public abstract class AbsCircleFragment<T,VH extends BaseViewHolder> extends BaseLazyLoadFragment implements SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    protected LinearLayoutManager mLinearLayoutManager;

    @Override
    protected int initLayout() {
        return R.layout.fragment_record;
    }

    @Override
    protected void initView(View view) {
        mLinearLayoutManager = new ExLinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mLinearLayoutManager);
        mAdapter = newIntanceAdapter();
        recyclerView.setItemAnimator(null);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.status_center_empty_view);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                requestData(currentPage);
            }
        });
    }

    protected abstract BaseQuickAdapter<T,VH> newIntanceAdapter();

    protected BaseQuickAdapter<T, VH> mAdapter;

    @Override
    public void onRefresh() {
        if(mAdapter instanceof CircleHomeAdapter){
            ((CircleHomeAdapter)mAdapter).cleanUserCache();
        }
//        else if(mAdapter instanceof UserCircleFriendsAdapter){
//            ((UserCircleFriendsAdapter)mAdapter).cleanUserCache();
//        }

        currentPage = 1;
        requestData(currentPage);
    }

    @Override
    public void onLoadMore() {
        currentPage++;
        requestData(currentPage);
    }

    protected abstract void requestData(int page);

    protected int PAGE_COUNT(){
        return 20;
    }
    protected int currentPage = 1;

    protected void finishRefreshAndLoad(int page, List<T> objects){
        if(page == 1){
            swipeRefreshLayout.setRefreshing(false);
            mAdapter.setNewInstance(objects);
        }else{
            if(objects!=null&&objects.size()>0){
                mAdapter.addData(objects);
            }
        }
        if(Common.isEmpty(objects)){
            mAdapter.getLoadMoreModule().loadMoreEnd(true);
        }else{
            mAdapter.getLoadMoreModule().loadMoreComplete();
        }
    }
}
